﻿module Sustainalytics.EsgRatings {

    import Company = Sustainalytics.Company;

    export class RelativePositionController {

        public esgRelativePosition: EsgRelativePositionDto;
        public esgRelativePosition_positionSummary_esgPositionName: string;


        constructor(
            private $scope: Company.ICompanyBasedScope,
            private $q: ng.IQService,
            private $timeout: ng.ITimeoutService,
            private $filter: ng.IFilterService,
            private EsgRelativePositionService: IEsgRelativePositionService,
            private NumberUtilityService: Common.NumberUtilityService,
            private $rootScope) {

            var ctx = this;
            ctx.init();
        }

        private init(): ng.IPromise<any> {
            var ctx = this;

            var promise = ctx.getEsgRelativePosition();

            return promise;
        }

        private getEsgRelativePosition(): ng.IPromise<EsgRelativePositionDto> {
            var ctx = this;
            var promise = ctx.EsgRelativePositionService.get(ctx.$scope.currentCompany.id);

            promise.then(function (res) {
                ctx.$timeout(function () {
                    ctx.$scope.$apply(function () {
                        ctx.esgRelativePosition = res;
                        ctx.esgRelativePosition_positionSummary_esgPositionName = ctx.getEsgPositionName(ctx.esgRelativePosition.positionSummary.esgPosition);
                    });
                });
            },() => {

                    ga('create', 'UA-4379897-6', {
                        'userId': ctx.$rootScope.userInfo.userId, 'cookieDomain': 'none', 'name': 'globalAccess'
                    });

                    ga('globalAccess.send', 'exception', { 'exDescription': 'Esg Ratings: Relative Position Controller -> Error loading Relative Position' });
                });

            return promise;
        }

        public getEsgPosition(): any {
            return EsgPositionEnum;
        }

        public getEsgPositionName(esgPosition: EsgPositionEnum): string {
            return EsgUtility.getEsgPositionName(esgPosition);
        }
    }


    export var relativePositionControllerName = 'RelativePositionController';

    angular.module(esgRatingsModuleName).controller(relativePositionControllerName, RelativePositionController);
} 